<?php 
namespace App\Models;
use App\Cache\BaseCache;

class Category extends BaseModel{
	
	//表明
	protected $table = 'category';
	//主键
	protected $primaryKey = 'category_id';
    
    public function getCategoryObject(){
        $categoryObject = array();
        $categoryObject['categoryId'] = $this->id;
        $categoryObject['name'] = $this->name;
        $categoryObject['sort'] = $this->sort;
        return  $categoryObject;
    }
    
    public function getCategoryLive(){
        $modelCache = new BaseCache();
        $modelCache->method = 'get';
        $modelCache->cacheName = 'categoryLive';
        $retCache = $modelCache->HandleCache();
        if ($retCache === 'CACHE_BREAKDOWN') {
            return array();
        }
        $data = array();
        if (is_array($retCache) && count($retCache) > 0) {
            $data = $retCache;
        } else {
            $categorys = $this->select('id','name', 'status','sort', 'is_delete')
                    ->where('status', '=', 1)
                    ->where('is_delete', '=', 0)
                    ->orderBy('sort', 'asc')
                    ->get();
            foreach ($categorys as $category) {
                $data[] = $category->getCategoryObject();
            }
            if(!$data){
                $data = 'CACHE_BREAKDOWN';
            }
            $modelCache->cacheValue = $data;
            $modelCache->method = 'put';
            $modelCache->HandleCache();
        }
        if (!is_array($data)) {
            $data = array();
        }
        return $data;
    }
}